package cn.iocoder.yudao.module.enterprise.controller.admin.account;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.enterprise.controller.admin.account.vo.*;
import cn.iocoder.yudao.module.enterprise.convert.account.AccountConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.account.AccountDO;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.account.AccountSourceDO;
import cn.iocoder.yudao.module.enterprise.service.account.AccountService;
import cn.iocoder.yudao.module.enterprise.service.account.AccountSourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 碳排放核算")
@RestController
@RequestMapping("/enterprise/account")
@Validated
public class AccountController {

    @Resource
    private AccountService accountService;

    @Resource
    private AccountSourceService accountSourceService;

    @PostMapping("/create")
    @Operation(summary = "创建碳排放核算")
    @PreAuthorize("@ss.hasPermission('enterprise:account:create')")
    public CommonResult<Long> createAccount(@Valid @RequestBody AccountCreateReqVO createReqVO) {
        return success(accountService.createAccount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新碳排放核算")
    @PreAuthorize("@ss.hasPermission('enterprise:account:update')")
    public CommonResult<Boolean> updateAccount(@Valid @RequestBody AccountUpdateReqVO updateReqVO) {
        accountService.updateAccount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除碳排放核算")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('enterprise:account:delete')")
    public CommonResult<Boolean> deleteAccount(@RequestParam("id") Long id) {
        accountService.deleteAccount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得碳排放核算")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('enterprise:account:query')")
    public CommonResult<AccountRespVO> getAccount(@RequestParam("id") Long id) {
        AccountDO account = accountService.getAccount(id);
        return success(AccountConvert.INSTANCE.convert(account));
    }

    @GetMapping("/page")
    @Operation(summary = "获得碳排放核算分页")
    @PreAuthorize("@ss.hasPermission('enterprise:account:query')")
    public CommonResult<PageResult<AccountRespVO>> getAccountPage(@Valid AccountPageReqVO pageVO) {
        PageResult<AccountDO> pageResult = accountService.getAccountPage(pageVO);
        return success(AccountConvert.INSTANCE.convertPage(pageResult));
    }

    @PostMapping("/create-source")
    @Operation(summary = "创建碳排放核算的排放源")
    @PreAuthorize("@ss.hasPermission('enterprise:account:create')")
    public CommonResult<Long> createAccountSource(@Valid @RequestBody AccountSourceCreateReqVO createReqVO) {
        return success(accountSourceService.createAccountSource(createReqVO));
    }

    @PutMapping("/update-source")
    @Operation(summary = "更新碳排放核算的排放源")
    @PreAuthorize("@ss.hasPermission('enterprise:account:update')")
    public CommonResult<Boolean> updateAccountSource(@Valid @RequestBody AccountSourceUpdateReqVO updateReqVO) {
        accountSourceService.updateAccountSource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete-source")
    @Operation(summary = "删除碳排放核算的排放源")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('enterprise:account:delete')")
    public CommonResult<Boolean> deleteAccountSource(@RequestParam("id") Long id) {
        accountSourceService.deleteAccountSource(id);
        return success(true);
    }

    @GetMapping("/page-source")
    @Operation(summary = "获得碳排放核算的排放源分页")
    @PreAuthorize("@ss.hasPermission('enterprise:account:query')")
    public CommonResult<PageResult<AccountSourceRespVO>> getAccountSourcePage(@Valid AccountSourcePageReqVO pageVO) {
        return success(accountSourceService.getAccountSourcePage(pageVO));
    }

    @PostMapping("/source")
    @Operation(summary = "获得排放源的排放总量")
    @PreAuthorize("@ss.hasPermission('enterprise:account:query')")
    public CommonResult<Double> getEmissionAccount(@Valid @RequestBody AccountEmissionReqVO reqVO) {
        return success(accountSourceService.getEmissionAccount(reqVO));
    }

    @PostMapping("/simulation-list")
    @Operation(summary = "碳排放仿真-排放情况列表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('enterprise:account:query')")
    public CommonResult<AccountSimulationRespVO> simulationList(@RequestParam("id") Long id) {
        AccountDO account = accountService.getAccount(id);
        List<AccountSourceDO> accountSourceList = accountSourceService.getAccountSourceListByAccountId(account.getId());
        List<AccountSimulationReqVO.SourceSimulation> sourceSimulationList = new ArrayList<>();
        accountSourceList.forEach(x -> {
            AccountSimulationReqVO.SourceSimulation sourceSimulation = new AccountSimulationReqVO.SourceSimulation();
            sourceSimulation.setSourceId(x.getSourceId())
                    .setDataValue(x.getDataValue());
            sourceSimulationList.add(sourceSimulation);
        });
        return success(accountSourceService.simulationAccount(sourceSimulationList));
    }

    @PostMapping("/simulation-update-source")
    @Operation(summary = "碳排放仿真-修改排放源的活动数据")
    @PreAuthorize("@ss.hasPermission('enterprise:account:query')")
    public CommonResult<AccountSimulationRespVO> simulationUpdateSource(@Valid @RequestBody AccountSimulationReqVO simulationReqVO) {
        List<AccountSimulationReqVO.SourceSimulation> sourceSimulationList = simulationReqVO.getSourceList();
        return success(accountSourceService.simulationAccount(sourceSimulationList));
    }

}
